﻿Imports System.Windows.Forms
Imports HMSLibrary
Imports System.Collections.ObjectModel
Imports WPFSpark
Imports System.Windows.Threading
Imports HMSDataLayer

Public Class MainWindow

    Dim timer As New DispatcherTimer

    Private Sub timerTick(ByVal sender As Object, ByVal e As EventArgs)
        DateTimeRun.Text = Format(Date.Now, "dd-MMM-yyyy HH:mm:ss")
    End Sub

    Private Sub MainWindow_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.Height = Screen.PrimaryScreen.WorkingArea.Height
        Me.Width = Screen.PrimaryScreen.WorkingArea.Width
    End Sub

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If AppCommon.IsRegsiter = False Then
            RegistrationInfoRun.Cursor = Input.Cursors.Hand
            RegistrationInfoRun.Foreground = New SolidColorBrush(Colors.Blue)
        Else
            RegistrationInfoRun.Text = "License to : " & AppCommon.LicenseTo
        End If

        Dim metro As New Metrolizer
        metro.DisplayTiles(MetroStackPanel, AppCommon.loginInfo.ModulesList, AppCommon.loginInfo.IsSuperUser)

        Dim path As String = My.Settings.SkinPath
        Dim newDictionary As New ResourceDictionary()
        newDictionary.Source = New Uri(path, UriKind.Relative)
        Me.Resources.MergedDictionaries.Clear()
        Me.Resources.MergedDictionaries.Add(newDictionary)

        timer.Interval = TimeSpan.FromSeconds(1)
        AddHandler timer.Tick, AddressOf timerTick
        timer.Start()
        UserNameRun.Text = AppCommon.loginInfo.UserName
        CompanyNameRun.Text = AppCommon.loginInfo.CompanyName

        If AppCommon.loginInfo.IsSuperUser = False Then
            Dim mp As ModulePermissions
            mp = (From ml In AppCommon.loginInfo.ModulesList Where ml.Name = "Reports" Select ml).FirstOrDefault
            If mp.View = True Then
                LoadLeftSidePanel()
            Else
                ReportGrid.Visibility = Visibility.Collapsed
            End If
            mp = (From ml In AppCommon.loginInfo.ModulesList Where ml.Name = "Charts" Select ml).FirstOrDefault
            If mp.View = True Then
                LoadChartData()
            Else
                ChartGrid.Visibility = Visibility.Collapsed
            End If
        Else
            LoadLeftSidePanel()
            LoadChartData()
        End If
    End Sub

    Private Sub LoadChartData()
        Dim lst As List(Of ChartData)
        Dim cdl As New CommonDataLayer()
        lst = cdl.GetSaleChartData(Date.Now.Year)
        columnChartIncome.DataContext = lst

        lst = cdl.GetCollectionChartData(Date.Now.Year)
        columnChartCollection.DataContext = lst

        lst = cdl.GetPatientInflowChartData(Date.Now.Year)
        columnChartPatientFlow.DataContext = lst
    End Sub

    Private Sub LoadLeftSidePanel()
        Dim items As New ObservableCollection(Of PivotItem)
        Dim modules As String() = New String() {"patient", "billing", "purchase", "registers", "inventory"}
        Dim brushes__1 As Brush() = New Brush() {Brushes.Red, Brushes.LawnGreen, Brushes.Blue, Brushes.Orange, Brushes.Cyan, Brushes.AntiqueWhite, Brushes.BlanchedAlmond}
        Dim data As New List(Of List(Of ModuleInfo))()
        Dim lstPatient As New List(Of ModuleInfo)
        lstPatient.Add(New ModuleInfo With {.ModuleTitle = "patient details", .ModuleDescription = "patient history"})
        data.Add(lstPatient)

        Dim lstBilling As New List(Of ModuleInfo)
        lstBilling.Add(New ModuleInfo With {.ModuleTitle = "billing details", .ModuleDescription = "billing details"})
        data.Add(lstBilling)

        Dim lstPurchase As New List(Of ModuleInfo)
        lstPurchase.Add(New ModuleInfo With {.ModuleTitle = "bill details", .ModuleDescription = "purchase bill details"})
        data.Add(lstPurchase)

        Dim lstRegisters As New List(Of ModuleInfo)
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "patients", .ModuleDescription = "patient register"})
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "doctors", .ModuleDescription = "doctor register"})
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "bills", .ModuleDescription = "sales register"})
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "purchase", .ModuleDescription = "purchase register"})
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "vendors", .ModuleDescription = "vendor register"})
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "rooms", .ModuleDescription = "room register"})
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "services", .ModuleDescription = "service register"})
        lstRegisters.Add(New ModuleInfo With {.ModuleTitle = "items", .ModuleDescription = "items register"})
        data.Add(lstRegisters)

        Dim lstInventory As New List(Of ModuleInfo)
        lstInventory.Add(New ModuleInfo With {.ModuleTitle = "sale stock", .ModuleDescription = "sale stock details"})
        lstInventory.Add(New ModuleInfo With {.ModuleTitle = "purchase stock", .ModuleDescription = "purchase stock details"})
        lstInventory.Add(New ModuleInfo With {.ModuleTitle = "inventory status", .ModuleDescription = "inventory status details"})
        lstInventory.Add(New ModuleInfo With {.ModuleTitle = "reordering", .ModuleDescription = "reorder items details"})
        data.Add(lstInventory)

        For i As Integer = 0 To modules.Count() - 1
            Dim tb As New PivotHeaderControl()
            tb.FontFamily = New FontFamily("Segoe WP")
            tb.FontWeight = FontWeights.Light
            tb.ActiveForeground = Brushes.Green
            tb.InactiveForeground = New SolidColorBrush(Color.FromRgb(48, 48, 48))
            tb.FontSize = 24
            tb.Content = modules(i)
            tb.Margin = New Thickness(20, 0, 0, 0)

            Dim pci As New PivotContentControl()
            Dim lb As New System.Windows.Controls.ListBox() With { _
              .FontFamily = New FontFamily("Segoe WP"), _
              .FontSize = 16, _
              .FontWeight = FontWeights.Light, _
              .Foreground = Brushes.Gray, _
              .Background = TryFindResource("TileBgRctFill"), _
              .BorderThickness = New Thickness(0) _
            }
            AddHandler lb.MouseDoubleClick, AddressOf lst_MouseDoubleClick
            lb.ItemTemplate = DirectCast(Me.Resources("ListBoxItemTemplate"), DataTemplate)
            lb.ItemsSource = data(i)
            ScrollViewer.SetHorizontalScrollBarVisibility(lb, ScrollBarVisibility.Disabled)
            lb.HorizontalAlignment = HorizontalAlignment.Stretch
            lb.VerticalAlignment = VerticalAlignment.Stretch
            lb.Margin = New Thickness(15, 5, 5, 5)
            pci.Content = lb

            Dim pi As New PivotItem() With { _
              .PivotHeader = tb, _
              .PivotContent = pci _
            }
            items.Add(pi)
        Next

        RootPivotPanel.ItemsSource = items

        RootPivotPanel.Background = TryFindResource("TileBgRctFill")
    End Sub

    Private Sub lst_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim mInfo As ModuleInfo
        mInfo = DirectCast(DirectCast(sender, System.Windows.Controls.ListBox).SelectedItem, HMSLibrary.ModuleInfo)
        If mInfo Is Nothing Then Exit Sub
        Dim PrintForm As New ReportViewerWindow
        If mInfo.ModuleTitle = HMSReports.Bills Then
            PrintForm.ReportType = ReportsList.RegisterBill
        ElseIf mInfo.ModuleTitle = HMSReports.Patients Then
            PrintForm.ReportType = ReportsList.RegisterPatient
        ElseIf mInfo.ModuleTitle = HMSReports.Complaints Then
            PrintForm.ReportType = ReportsList.RegisterComplaints
        ElseIf mInfo.ModuleTitle = HMSReports.Doctors Then
            PrintForm.ReportType = ReportsList.RegisterDoctors
        ElseIf mInfo.ModuleTitle = HMSReports.Employee Then
            PrintForm.ReportType = ReportsList.RegisterEmp
        ElseIf mInfo.ModuleTitle = HMSReports.Items Then
            PrintForm.ReportType = ReportsList.RegisterItems
        ElseIf mInfo.ModuleTitle = HMSReports.Employee Then
            PrintForm.ReportType = ReportsList.RegisterEmp
        ElseIf mInfo.ModuleTitle = HMSReports.Services Then
            PrintForm.ReportType = ReportsList.RegisterServices
        ElseIf mInfo.ModuleTitle = HMSReports.Vendors Then
            PrintForm.ReportType = ReportsList.RegisterVendors
        Else
            PrintForm.ReportType = ReportsList.None
        End If
        PrintForm.Show()
    End Sub

    Private Sub MainWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        DragMove()
    End Sub

    Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        End
    End Sub

    Private Sub MinimizeButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MinimizeButton.Click
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Private Sub MetroStackPanel_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles MetroStackPanel.MouseLeftButtonDown
        Dim txt As TextBlock = DirectCast(DirectCast(DirectCast(e.Source, HMSLibrary.Tile).Content, System.Windows.Controls.Grid).Children(1), System.Windows.Controls.TextBlock)
        If txt.Text = HMSFormName.Billing Then
            Dim saleBillWindow As New BillingWindow
            saleBillWindow.Owner = Me
            saleBillWindow.Show()
        ElseIf txt.Text = HMSFormName.PatientDischarge Then
            Dim window As New PatientDischargeWindow
            window.Owner = Me
            window.Show()
        ElseIf txt.Text = HMSFormName.Purchase Then
            Dim PurchaseWindow As New PurchaseWindow(FormsList.Purchase)
            PurchaseWindow.Owner = Me
            PurchaseWindow.Show()
        ElseIf txt.Text = HMSFormName.PurchaseOrder Then
            Dim PurchaseOrderWindow As New PurchaseWindow(FormsList.PurchaseOrder)
            PurchaseOrderWindow.Owner = Me
            PurchaseOrderWindow.Show()
        ElseIf txt.Text = HMSFormName.PatientRegistration Then
            Dim PatientRegWindow As New PatientRegistrationWindow
            PatientRegWindow.Owner = Me
            PatientRegWindow.Show()
        ElseIf txt.Text = HMSFormName.PatientAdmission Then
            Dim PatientAdmWindow As New PatientAdmissionWindow
            PatientAdmWindow.Owner = Me
            PatientAdmWindow.Show()
        ElseIf txt.Text = HMSFormName.PatientVisit Then
            Dim PatientVisitWindow As New PatientVisitWindow
            PatientVisitWindow.Owner = Me
            PatientVisitWindow.Show()
        ElseIf txt.Text = HMSFormName.NextAppointment Then
            Dim Win As New AppointmentWindow(0)
            Win.FormType = FormsList.NextAppointment
            Win.Owner = Me
            Win.Show()
        ElseIf txt.Text = HMSFormName.aboutUs Then

        Else
            Dim master As New MasterWindow
            master.Owner = Me
            Select Case txt.Text
                Case HMSFormName.Banks
                    master.FormType = FormsList.Banks
                Case HMSFormName.Employee
                    master.FormType = FormsList.Employee
                Case HMSFormName.Departments
                    master.FormType = FormsList.Departments
                    'Case HMSFormName.Complaints
                    '    master.FormType = FormsList.Complaints
                Case HMSFormName.Doctors
                    master.FormType = FormsList.Doctors
                Case HMSFormName.Diagnosis
                    master.FormType = FormsList.Diagnosis
                Case HMSFormName.CompanyInfo
                    master.FormType = FormsList.CompanyInfo
                Case HMSFormName.ItemsServices
                    master.FormType = FormsList.ItemsServices
                Case HMSFormName.Lookups
                    master.FormType = FormsList.Lookup
                Case HMSFormName.UserProfile
                    master.FormType = FormsList.UserProfile
                Case HMSFormName.UsersList
                    master.FormType = FormsList.Users
                Case HMSFormName.Vendors
                    master.FormType = FormsList.Vendor
                Case HMSFormName.Rooms
                    master.FormType = FormsList.Rooms
                Case HMSFormName.Wards
                    master.FormType = FormsList.Wards
                Case HMSFormName.billTemplates
                    master.FormType = FormsList.BillTemplate
                Case HMSFormName.Medicines
                    master.FormType = FormsList.Medicines
                Case HMSFormName.Findings
                    master.FormType = FormsList.Findings
                Case HMSFormName.Investigations
                    master.FormType = FormsList.Investigations
                Case HMSFormName.PrescriptionTemplate
                    master.FormType = FormsList.PrescriptionTemplate
                Case HMSFormName.Problems
                    master.FormType = FormsList.Problems
                    'Case HMSFormName.TreatmentProcedures
                    '    master.FormType = FormsList.TreatmentProcedures
            End Select
            master.Show()
        End If
        e.Handled = True
    End Sub

    Private Sub RegistrationInfoRun_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        If RegistrationInfoRun.Text = "Trial Version" Then
            Dim abt As New AboutUsWindow
            abt.Owner = Me
            abt.ShowDialog()
        End If
    End Sub
End Class
